// 地址相关操作
import { get, post, put, del } from '@common/ajax';
import { createSign } from '@common/utils';
import urls, { MEMBER_URL } from '@common/url';
import {
    GET_ADDRESS_LIST,
    SEARCH_ADDRESS,
    SET_EDIT_ADDRESS,
    RESET_SELECT_ADDRESS,
} from '@store/mutationType';
import logger from '@common/logger';
const address = {
    state: {
        addressList: [],
        editAddressInfo: null,
        searchList: [],
        selectedInfo: {
            address: '',
            latitude: null,
            longitude: null,
        },

    },
    mutations: {
        GET_ADDRESS_LIST: (state, addressList) => {
            Object.assign(state, { addressList });
        },
        SET_EDIT_ADDRESS: (state, editAddressInfo) => {
            Object.assign(state, { editAddressInfo });
        },
        SEARCH_ADDRESS: (state, searchList) => {
            Object.assign(state, { searchList });
        },
    },
    actions: {
        // 获取地址列表
        getAddressList: async ({ commit }) => {
            const res = await get(MEMBER_URL.addressList);
            logger.debugger('Address => 获取的地址列表', res);
            commit(GET_ADDRESS_LIST, res.data.addresses);
        },
        // 添加收货地址
        addAddressItem: async ({ dispatch }, item) => {
            const res = await post(MEMBER_URL.addressCreate, item);
            dispatch('getAddressList');
            return res;
        },
        // 更新收货地址
        updateAddressItem: async ({ dispatch }, item) => {
            const res = await put(MEMBER_URL.addressUpdate, item);
            dispatch('getAddressList');
            return res;
        },
        // 删除收货地址
        deleteAddressItem: async ({ dispatch }, id) => {
            await del(MEMBER_URL.addressDelete(id));
            dispatch('getAddressList');
        },
        // 设置编辑的地址信息
        setEditAddressInfo: ({ commit, state }, id) => {
            const { addressList } = state;
            const _arr = addressList.filter(item => item.addr_id === id);
            logger.debugger('Address => 设置编辑地址信息', _arr[0]);
            commit(SET_EDIT_ADDRESS, _arr[0]);
        },
        // 地址匹配解析
        parseAddress: async ({ commit }, param) => {
            const res = await get(MEMBER_URL.addressParse, param);
            logger.debugger('Address => 地址解析结果', res);
            return res;
        },
        // 通过id获取单个地址信息
        getAddressItem: ({ state }, id) => {
            const { addressList } = state;
            const _arr = addressList.filter(item => item.id === id);
            return _arr.length > 0 ? _arr[0] : null;
        },
        // 根据文字输入搜索地址
        searchAddress: async ({ commit }, param) => {
            const res = await get(MEMBER_URL.search, param);
            logger.debugger('Address => 搜索结果', res.data.addresses);
            commit(SEARCH_ADDRESS, res.data.addresses);
        },
        // 清空编辑数据
        resetEditAddressInfo: ({ commit }) => {
            commit(SET_EDIT_ADDRESS, null);
        },
        // // 根据经纬度获取地址
        // matchAddress: async ({ commit, dispatch }, param) => {
        //     const res = await get(urls.addressMatch, param);
        //     logger.info('获取到的经纬度信息为：', res);
        //     if (res) {
        //         dispatch('setLocationInfo', res);
        //         commit(GET_MATCH_ADDRESS, res.address);
        //         commit(SET_MATCH_ADDRESS_INFO, {
        //             address: res.address,
        //             latitude: res.latitude,
        //             longitude: res.longitude,
        //         });
        //     }
        // },
        // // 校验地址是否是在配送范围
        // checkAddressIsCanUse: async ({ state, commit }, param) => {
        //     const res = await get(urls.addressCheck, param);
        //     commit(SET_DELIVERY_PRICE_INFO, res.delivery_regions);
        //     return res.can_deliver;
        // },

        // setSelectValue: ({ commit }, param) => {
        //     commit(SET_SELECT_ADDRESS, {
        //         address: param.address || '',
        //         latitude: param.latitude || null,
        //         longitude: param.longitude || null,
        //     });
        // },

        // // 修改当前定位信息
        // setLocationInfo: ({ commit }, param) => {
        //     logger.info('当前设置的地址信息参数为：', param);
        //     commit(SET_LOCATION_INFO, {
        //         address: param.title || param.address || '',
        //         latitude: param.latitude || null,
        //         longitude: param.longitude || null,
        //     });
        // },
        // // 设置外卖配送地址信息
        // setDeliveryAddressInfo: ({ commit }, info) => {
        //     commit(SET_DELIVERY_ADDRESS_INFO, info);
        // },
    },
};
export default address;
